<?php // !class_exists('ujn_model', false) or die('who are u?');

class mdl_bottle_item extends ujn_model {


	public function getAllCount() {
		$sql = "select count(1) c from `{$this->db_name}`.`{$this->tbl_name}`;";
		$c = $this->_db->getOne($sql);
		return isset($c['c']) ? (int)$c['c'] : 0;
	}


	public function find_unpick_bottle() {
		$sql = "select count(1) c from `{$this->db_name}`.`{$this->tbl_name}` where `receive_owner_uid` = '0';";
		$d = $this->_db->getOne($sql);
		return isset($d['c']) ? (int)$d['c'] : 0;
	}

	public function find_unsess_bottle($xday = 3) {
		$last_receive_updated = TODAY - 86400 * $xday;
		$sql =<<<EOT
select * from `{$this->db_name}`.`{$this->tbl_name}`
where `receive_owner_uid` != '0' and `sid` = '0'
and `receive_updated` < '{$last_receive_updated}'
order by `receive_updated`
limit 100;
EOT;
		return $this->_db->getAll($sql);
	}

	/**
	 *
	 * 默认是收到的
	 * @param unknown_type $uid
	 * @param unknown_type $is_receive
	 */
	public function findCount($uid, $is_receive, $time) {
		if(0 == (int)$is_receive) {
			$query_field	= 'receive_owner_uid';
			$query_field2	= 'receive_updated';
		} else {
			$query_field	= 'uid';
			$query_field2	= 'created';
		}

		$sql =<<<EOT
select count(1) c from `{$this->db_name}`.`{$this->tbl_name}`
where `{$query_field}` = '{$uid}' and `{$query_field2}` > '{$time}';
EOT;
		$res = $this->_db->getOne($sql);
		return isset($res['c']) ? (int)$res['c'] : 0;
	}

	public function findItems($uid, $is_receive, $offset, $max) {

		$offset		 = (int)$offset;
		$max		 = ( (int)$max == 0) ? 10 : (int)$max;
		$query_field = 0 == (int)$is_receive ? 'receive_owner_uid' : 'uid';

		$sql =<<<EOT
select `iid`, `content`, `created`, `receive_updated`, `session_updated`, `session_uid`
from {$this->tbl_name}
where `{$query_field}` = '{$uid}'
order by session_updated desc, created desc
limit {$offset}, {$max};
EOT;
		$res = $this->_db->getAll($sql);
		return $res;
	}

	public function findOne($iid) {
		$sql =<<<EOT
select `iid`, `uid`, `content`, `created`, `receive_owner_uid`, `receive_updated`, `session_updated`, `session_uid`
from {$this->tbl_name}
where `iid` = '{$iid}' limit 1;
EOT;
		$res = $this->_db->getOne($sql);
		return $res;
	}

	public function findOne_rand($uid, $without_rand = false) {
		if(!$without_rand && mt_rand(0, 5) == 0) return false; // 1/5 几率失敗

		$sql =<<<EOT
select `iid`, `uid`, `content`, `created`, `receive_updated`, `session_updated`, `session_uid`
from {$this->tbl_name}
where `uid` != '{$uid}'
	and `receive_owner_uid` = '0'
order by rand()
limit 1;
EOT;
// and `created` < unix_timestamp() - 60*15
		$res = $this->_db->getOne($sql);
		return $res;
	}

	public function makeOne($uid, $content) {
		$sql =<<<EOT
insert into {$this->tbl_name} set
`uid` = '{$uid}', `content` = '{$content}',
`created` = unix_timestamp(), `receive_owner_uid` = '0', `receive_updated` = '0', `session_updated` = '0', `session_uid` = '0';
EOT;
		$this->_db->query($sql);
		return $this->_db->insertID();
	}

	public function setReceiveOwner($iid, $uid = '0') {

		$icount = 1;
		if(is_array($iid)) {
			$icount = count($iid);
			if($icount == 0) return false;
			$_iid = array();
			foreach($iid as $_) {
				$_iid[] = "'{$_}'";
			}

			$_iid = '(' . implode(',', $_iid) . ')';
			$iid = "`iid` in $_iid";
		}

		else $iid = "`iid` = '{$iid}'";

		$sql =<<<EOT
update {$this->tbl_name}
set `receive_owner_uid` = '{$uid}', `receive_updated` = unix_timestamp()
where {$iid} and `sid` = '0'
limit {$icount};
EOT;
		$this->_db->query($sql);
		return $this->_db->affectedRows() > 0;
	}

	public function updateSession($iid, $suid) {
		$sql =<<<EOT
update {$this->tbl_name}
set `session_updated` = unix_timestamp(), `session_uid` = '{$suid}'
where `iid` = '{$iid}'
limit 1;
EOT;
		$this->_db->query($sql);
		return $this->_db->affectedRows() > 0;
	}

	protected function __construct() {
		parent::__construct(__CLASS__);
		$this->_db	= $this->mysql();
	}

    /**
     * @return mdl_bottle_item
     */
	static function getInstance() {
		return parent::getInstance(__CLASS__);
	}
}